#include "mainscene.h"
#include "ui_mainscene.h"
#include "mypushbutton.h"
#include<QPainter>
#include<QDebug>
#include<QTimer>
#include<QSound>//音效
Mainscene::Mainscene(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::Mainscene)
{
    ui->setupUi(this);
    //设置图标
    setWindowIcon(QIcon(":/res/Coin0001.png"));
    //设置标题
    setWindowTitle("翻金币主场景");
    //退出
    connect(ui->actionQuit,&QAction::triggered,[=](){
        this->close();
    });

    //准备开始按钮的音效
    QSound *startSound=new QSound(":/res/TapButtonSound.wav",this);
//    startSound->setLoops(-1);//无限循环播放
//    startSound->play();

    //开始按钮
   MyPushButton *startBtn =new MyPushButton(":/res/MenuSceneStartButton.png");
   startBtn->setParent(this);
   startBtn->move(this->width()*0.5-startBtn->width()*0.5,this->height()*0.7);

   choseScene =new ChooseLevelScene;
   //监听按钮返回信号
   connect(choseScene,&ChooseLevelScene::ChooseSceneBack,[=](){
       this->setGeometry(choseScene->geometry());
       choseScene->hide();
       this->show();
   });
   connect(startBtn,&MyPushButton::clicked,[=](){
       //播放开始音效
       startSound->play();
       //按钮弹起样式
       startBtn->zoom1();
       startBtn->zoom2();
       //延时进入到选择关卡
       QTimer::singleShot(50,this,[=](){
           choseScene->setGeometry(this->geometry());
           //只剩隐藏
           this->hide();
           //进入选择关卡场景中
           choseScene->show();   
      } );

   });
}

Mainscene::~Mainscene()
{
    delete ui;
}

void Mainscene::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    QPixmap pix;
    pix.load(":/res/PlayLevelSceneBg.png");
    painter.drawPixmap(0,0,this->width(),this->height(),pix);

    //画背景上的图标
    pix.load(":/res/Title.png");
    pix=pix.scaled(pix.width()*0.5,pix.height()*0.5);
    painter.drawPixmap(10,30,pix);

}
